import wxRequest from "../../utils/wxRequest";

// pages/login/login.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imagesUrl: app.globalData.imagesUrl,
    remmenber: 1,
    user: "",
    pwd: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var uid = wx.getStorageSync('rmid');
    if (uid) {
      wx.switchTab({
        url: '../mine/mine',
      })
    }

  },
  getuser: function (e) {
    console.log(e)
    var user = e.detail.value;
    this.setData({
      user: user
    });
  },
  getpwd: function (e) {
    var pwd = e.detail.value;
    this.setData({
      pwd: pwd
    });
  },
  remmenber: function () {
    var remmenber = this.data.remmenber;
    if (remmenber == 0) {
      this.setData({
        remmenber: 1
      });
    } else {
      this.setData({
        remmenber: 0
      });
    }
  },
  login: function () {
    var user = this.data.user;
    var pwd = this.data.pwd;
    var remmenber = this.data.remmenber;
    var option = {
      url: "index/login",
      data: {
        user: user,
        pwd: pwd
      }
    };
    wxRequest(option).then(function (result) {
      var data = result.data;
      if (data.code == 1) {
        if (remmenber == 1) {
          wx.setStorageSync('rmid', data.uid);
        }
        wx.setStorageSync('uid', data.uid);
        wx.switchTab({
          url: '../mine/mine',
        })
      } else if (data.code == -1) {
        wx.showToast({
          title: data.msg,
          duration: 2000,
          icon: "none"
        })
      } else {
        wx.showToast({
          title: data.msg,
          duration: 2000,
          icon: "none"
        })
        this.onLoad();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})